﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Barry.BarbeSystem.Business;
using Barry.BarbeSystem.Core;
using Barry.BarbeSystem.Filters;
using Barry.BarbeSystem.Models;

namespace Barry.BarbeSystem.Controllers
{
    [CustomerAuthorizationFilter]
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        private IRepository<Employee> _employeeRepository;

        public EmployeeController()
        {
            _employeeRepository = new Repository<Employee>();
        }

        public ActionResult Index()
        {

            return View();
        }


        public JsonResult PageIndex(PageRequest request)
        {
            JsonResult jes = new JsonResult();


            var items = from item in _employeeRepository.GetAll()
                        orderby item.Name
                        select item;
            var itemCounts = items.Count();

            jes.Data = new GridResponseInfo() { rows = items.Skip((request.page - 1) * Utility.PageCount).Take(Utility.PageCount).ToList(), total = itemCounts };
            return jes;

        }


        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee e)
        {
            try
            {
                _employeeRepository.Save(e);
                return RedirectToAction("Index");
            }
            catch
            {
                return View(e);
            }
        }

        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Employee e = _employeeRepository.Get(id);
            if (e == null)
            {
                return HttpNotFound();
            }
            return View(e);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _employeeRepository.Update(model);

                    return RedirectToAction("Index");
                }

            }
            catch
            {

            }
            return View(model);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public JsonResult Delete(int id)
        {
            JsonResult j = new JsonResult();
            var obj = _employeeRepository.Get(id);
            if (obj != null)
            {

                _employeeRepository.Delete(obj);
                j.Data = new { success = true };

            }
            else
            {
                j.Data = new { success = false };
            }
            return j;
        }
    }
}
